/*(C) Copyright

    This file is part of GastosMovil.
    
    GastosMovil is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    GastosMovil is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with GastosMovil.  If not, see <http://www.gnu.org/licenses/>.
    
    Autor: Antonio Cristóbal Álvarez Abellán -> acabellan@gmail.com
    
    */

package deeloco.android.gastos.Movil.plus;


import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import android.util.Log;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;

public class Inicio extends Activity{
	/** Called when the activity is first created. */
	tarifas ts=new tarifas();
	private changeLog acerdaDe;

    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.inicio);
        ValoresPreferencias vp=new ValoresPreferencias(this);
        boolean delListadoLlamadas=false;
        try
        {
        	delListadoLlamadas=getIntent().getExtras().getBoolean("delListadoLlamadas",false);
        }
        catch (Exception e)
        {
        	delListadoLlamadas=false;
        }
        Log.d("....","delListadoLLamadas="+delListadoLlamadas);
        Log.d("....","Arrancar="+vp.getArrancarAplicacion());
        if ((vp.getArrancarAplicacion())&&(!delListadoLlamadas))
        {
			Intent i = new Intent(getBaseContext(), gastoMovil.class);
			i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        	startActivity(i);
        	finish();
        }

        GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(new ImageAdapter(this));

        ts.cargarTarifas();
        actualizarDatosResumenMes();
        
        acerdaDe = new changeLog(this,R.raw.acerdade);

        gridview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            	//Bundle extras = new Bundle();
            	v.setBackgroundColor(Color.rgb(12,11,12));
            	Intent i;
            	switch (position) {
				case 0://LISTADO DE LLAMADAS
					i = new Intent(getBaseContext(), gastoMovil.class);
		        	startActivity(i);
		        	break;
					
				case 1://CONFIGURAR TARIFAS
					//listado(vp.getPreferenciasMes());
					i = new Intent(getBaseContext(), PreferencesTarifas.class);
		        	startActivity(i);
		        	break;
		        	
				case 2: //AJUSTES
					//listado(vp.getPreferenciasMes());
					i = new Intent(getBaseContext(), Preferencias.class);
		        	startActivity(i);
		        	break;
		        	
				case 3: //ALARMAS
					i = new Intent(getBaseContext(), preferenciasAlarma.class);
		        	startActivity(i);
//					Toast.makeText(Inicio.this, getString(R.string.soloGMplus), Toast.LENGTH_SHORT).show();
					break;
					
				case 4: //ACERCA DE
					acerdaDe.setChangelog_full_title("Acerca De");
					acerdaDe.setChangelog_title("Acerca De");
					acerdaDe.getLogDialog().show();
			        
		        	break;
		        	
				case 5: //YOUTUBE
					startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.youtube.com/playlist?list=PL21193F9B9ECAB90C")));
			        
		        	break;

				default:
					Toast.makeText(Inicio.this, "Defecto "+position, Toast.LENGTH_SHORT).show();
					break;
				}
                
            }
        });
        //Fin gridview.setOnItemClickListener

      //Dialogo de log de nuevas características
        changeLog cl = new changeLog(this);
        if (cl.firstRun())
//        if (true)
            cl.getLogDialog().show();
        
    }
    

	/* (non-Javadoc)
	 * @see android.app.Activity#onActivityResult(int, int, android.content.Intent)
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode)
	    	{

	    	}
	}


	/* (non-Javadoc)
	 * @see android.app.Activity#onResume()
	 */
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
		//Actualizamos el dato de resumen del mes.
		actualizarDatosResumenMes();
	}
    
    
	/*
	 * Actualiza los datos de resumen del mes de la actividad inicio.
	 */
	private void actualizarDatosResumenMes()
	{
		//Datos de total gastado
		ValoresPreferencias vp=new ValoresPreferencias(this);
        TextView total=(TextView) findViewById(R.id.totalCabecera);
        String costeMes=vp.recuperarPreferencesString("costeTotalMes");
        costeMes=(costeMes==null)?"Sin Datos":costeMes+FunGlobales.monedaLocal();
        total.setText(vp.getPreferenciasMesTexto(0,false)+" .. "+costeMes);
	}
}
